package com.tomshushu.doc.freedoc.core.model.result;

import com.alibaba.fastjson.JSON;
import lombok.Data;

/**
 * @description: TODO 类描述
 * @Author guanqing
 * @Date 2021/4/27 16:42
 **/
@Data
public class BaseResult<T> {

    public static final String DEFAULT_SUCCESS_MESSAGE = "请求成功";

    public static final String DEFAULT_ERROR_MESSAGE = "网络异常";

    public static final String EXIT_USER_TEAM = "用户已在团队中";

    public static final String EXIT_USER_PROJECT = "用户已在项目中";

    public static final Integer DEFAULT_SUCCESS_CODE = 200;

    public static final Integer DEFAULT_ERROR_CODE = 500;

    /**
     * 请求是否成功
     */
    private Boolean success;

    /**
     * 响应状态码
     */
    private Integer code;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 响应对象
     */
    private T data;

    public BaseResult(){}

    public static <T> BaseResult<T> success(T t){
        BaseResult<T> br = new BaseResult<>();
        br.setSuccess(true);
        br.setCode(DEFAULT_SUCCESS_CODE);
        br.setMessage(DEFAULT_SUCCESS_MESSAGE);
        br.setData(t);
        return br;
    }

    public static <T> BaseResult<T> success(){
        return success(null);
    }

    public static <T> BaseResult<T> success(Integer code, String message) {
        return result(true, code, message, null);
    }

    public static <T> BaseResult<T> result(Boolean success, Integer code, String message, T t) {
        BaseResult<T> br = new BaseResult<>();
        br.setSuccess(success);
        br.setCode(code);
        br.setMessage(message);
        br.setData(t);
        return br;
    }

    public static <T> BaseResult<T> failure(Integer code, String message, T t) {
        return result(false, code, message, t);
    }

    public static <T> BaseResult<T> failure(Integer code, String message) {
        return result(false, code, message, null);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
